# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libvirt
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_pkgdev=7.10.0
pkgver=${_pkgdev//-/}
pkgrel=1
pkgdesc="Windows libvirt virtualization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url=https://libvirt.org
license=('LGPL2')
depends=(${MINGW_PACKAGE_PREFIX}-curl
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-gnutls
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-libgcrypt
         ${MINGW_PACKAGE_PREFIX}-libgpg-error
         ${MINGW_PACKAGE_PREFIX}-libssh
         ${MINGW_PACKAGE_PREFIX}-libssh2
         ${MINGW_PACKAGE_PREFIX}-libssp
         ${MINGW_PACKAGE_PREFIX}-libxml2
         ${MINGW_PACKAGE_PREFIX}-portablexdr
         ${MINGW_PACKAGE_PREFIX}-readline
         ${MINGW_PACKAGE_PREFIX}-yajl)
makedepends=(${MINGW_PACKAGE_PREFIX}-meson
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             ${MINGW_PACKAGE_PREFIX}-python
             ${MINGW_PACKAGE_PREFIX}-python-docutils
             ${MINGW_PACKAGE_PREFIX}-python-flake8
             "${MINGW_PACKAGE_PREFIX}-cc"
             rpcsvc-proto
             perl)
options=('strip' 'staticlibs')
source=("https://libvirt.org/sources/${_realname}-${_pkgdev}.tar.xz"{,.asc}
        001-fix-not-supported-authenticate-method.patch
        002-search-path.patch
        msys-rpcgen-as-portable.patch
        meson-drop-path-for-python.patch)
sha256sums=('cb318014af097327928c6e3d72922e3be02a3e6401247b2aa52d9ab8e0b480f9'
            'SKIP'
            '4635fd04e596879186b74ef9ddbacf64f6058e32bfd8d8376e1b7fa7838f679b'
            '38ced91a3a7f569ecaa4d738c1fe530604bf96ed26458559be398e30a2fa1829'
            '54eb6af16a1d001eab81dbc4352ff82a2349242d9f9f3f87eb45f75e8fc8a08d'
            'fda18a489451cee114a6f02fd561afe8731e1a30cf70b754bcca6434176859ad')
validpgpkeys=('453B65310595562855471199CA68BE8010084C9C') # Jiří Denemark <jdenemar@redhat.com>
noextract=(${_realname}-${_pkgdev}.tar.xz)

prepare() {
  [[ -d ${srcdir}/${_realname}-${_pkgdev%-*} ]] && rm -rf ${srcdir}/${_realname}-${_pkgdev%-*}
  MSYS=winsymlinks:native tar -xJf ${srcdir}/${_realname}-${_pkgdev}.tar.xz -C ${srcdir} || true

  cd ${_realname}-${_pkgdev%-*}
  patch -p1 -i ${srcdir}/001-fix-not-supported-authenticate-method.patch
  patch -p1 -i ${srcdir}/002-search-path.patch
  patch -p1 -i ${srcdir}/msys-rpcgen-as-portable.patch

  patch -p1 -R -i ${srcdir}/meson-drop-path-for-python.patch

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"
  sed -e "s|@@SEARCH_PATH@@|\'${_PRE_WIN}/bin,\'|g" -i meson.build
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  #CPPFLAGS+= -D_WIN32_WINNT=0x0501
  # https://github.com/msys2/MINGW-packages-dev/pull/6
  LDFLAGS+=" -fstack-protector"

  PYTHON=${MINGW_PREFIX}/bin/python.exe \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Drpath=disabled \
    -Ddriver_qemu=disabled \
    -Ddriver_openvz=disabled \
    -Ddriver_lxc=disabled \
    -Ddriver_vbox=disabled \
    -Ddriver_libvirtd=disabled \
    -Ddriver_esx=disabled \
    -Ddriver_hyperv=disabled \
    -Ddriver_vmware=disabled \
    -Dsasl=disabled \
    -Dpolkit=disabled \
    -Dnetcf=disabled \
    -Daudit=disabled \
    -Ddtrace=disabled \
    -Dtests=disabled \
    -Dexpensive_tests=disabled \
    ../${_realname}-${_pkgdev%-*}

  meson compile
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  meson test || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  DESTDIR="${pkgdir}" meson install

  install -Dm644 ${srcdir}/${_realname}-${_pkgdev%-*}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 ${srcdir}/${_realname}-${_pkgdev%-*}/COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
